import request from '../../request/request.js'
Page({

  data: {
    id:'',
    coachInfo:'',
    rateValue:'',
    rateDisable:false,
    content:'',
    commentList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
  //选择评分事件，每次改变触发，修改对应值
  onChange(event) {
    this.setData({
      rateValue: event.detail,
    });
  },
  onShow: function () {
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1];
    let options = currentPage.options;
    const { id } = options;
    this.setData({id:id})
    this.getDetail();
    this.getCommentList();
  },
  getDetail(){
    var that =this
    request({
      url:'coach/detail',
      method:'post',
      data:{id:that.data.id}
    }).then(res=>{
      this.setData({
        coachInfo :res.data.data
      })
    }).catch(err=>{})
  },
  submitRate(){
    var that = this
    if(this.data.rateDisable){
      //提示已经提交过
    }
    request({
      url:'coach/rate',
      method:'post',
      data:{
        id:that.data.id,
        grade:that.data.rateValue
      }
    }).then(res=>{
      console.log(res);
      if(res.data.success){
        that.getDetail();
      }
    }).catch(err=>{})
    //this.setData({rateDisable:true})
  },
  //输入评论后，离开焦点触发
  getContent(event){
    this.setData({content:event.detail.value})
  },
  submitComment(){
    let that = this
    let user = wx.getStorageSync("userInfo");
    request({
      url:'coach/comment',
      method:'post',
      data:{
        id:that.data.id,
        content:that.data.content,
        userId:user.id
      }
    }).then(res=>{
      if(res.data.success){
        that.getDetail();
        that.getCommentList();
      }
    }).catch(err=>{})
  },
  getCommentList(){
    let that = this
    request({
      url:'coach/comment/list',
      method:'post',
      data:{
        id:that.data.id,
      }
    }).then(res=>{
      if(res.data.success){
        that.setData({
          commentList:res.data.data
        })
      }
    })
  }
})